<?php

namespace app\index\controller;

use think\Controller;

class User extends Base {
	function index() {
        $lang = cookie ( 'think_var' ) ? cookie ( 'think_var' ) : '';
        $ratio = $this->inquiryCoin();
        $lang = strtolower($lang);
//        dd($ratio);
//        dd($lang);
        $map = [
            'ko'=>'KRW',
            'en-us'=>'USD',
            'es-es'=>'EUR',
            'fr-fr'=>'EUR',
            'ru-ru'=>'RUB',
            'zh-tw'=>'TWD',
            'zh-hk'=>'HKD',
            'zh-mo'=>'MOP',
            'pt-pt'=>'EUR',
            'bs'=>'CHF',
            'de'=>'EUR',
            'es'=>'EUR',
            'fa'=>'EUR',
            'teq'=>'TRY',
            'putao'=>'EUR',
            'ar-ae'=>'AED',
            'zh-cn'=>'CNY',
        ];

        $ratios = 0;
        if(isset($map[$lang]) && is_array($ratio)){
            $ratios = $ratio[$map[$lang]];
        }

//        'en-us','es-es','pt-pt','bs','ar-ae','zh-cn'

		$info = model ( 'Member' )->getInfo ( array ([ 'user_id','=',$this->uid ] ) );

        $info['ratio_money'] = $ratios * $info['money'];
        $info['ratio'] = $ratio[$map[$lang]];
        $info['asset'] = $map[$lang];

		$this->assign ( 'info', $info );
		return $this->fetch ();
	}

	function token() {
		$info = model ( 'Member' )->getInfo ( array ([ 'user_id','=',$this->uid ] ) );

		if ($this->request->isPost ()) {
			if ($this->request->param ( 'type' ) == 1)
				$data ['token'] = $this->request->param ( 'token' );
			else
				$data ['key'] = $this->request->param ( 'key' );
			$log = model ( 'Member' )->edit ( array ([ 'user_id','=',$this->uid ] ), $data );
			if ($log > 0)
				echo json_encode ( array ('code' => 98,'msg' => lang ( 'run_success' ) ) );
			else
				echo json_encode ( array ('code' => 97,'msg' => lang ( 'run_fail' ) ) );
		} else {
			$token = explode ( ',', $info ['token'] );
			$this->assign ( 'token', $token );
			$this->assign ( 'token_str', str_replace ( ',', ' ', $info ['token'] ) );
			$this->assign ( 'info', $info );
			return $this->fetch ();
		}
	}

    function inquiryCoin(){
        header("Content-type: text/html;charset=utf-8");
        define('CC_WEB_DEBUG', 1);
        if(CC_WEB_DEBUG){
            error_reporting(E_ALL & ~E_NOTICE);
            @ini_set("display_errors", "On");
        }else{
            error_reporting(E_ERROR | E_PARSE);
            @ini_set("display_errors", "Off");
        }
        @date_default_timezone_set('Asia/Shanghai');
        @ini_set('date.timezone','Asia/Shanghai');

        $UPApiConfig = get_config();
        $lang = cookie ( 'think_var' ) ? cookie ( 'think_var' ) : '';
        if ($lang == 'zh-cn'){
            $lan = "zh-CN";
        }else{
            $lan = "en-US";
        }

        $params = [
            'appid'=>$UPApiConfig['APPID'],
            'stamp'=>time(),
            "lang"=> $lan

        ];

        $paymentApi = new Upapi($UPApiConfig);
        $res = $paymentApi->inquiryCoin($params);
//        dd($res);
        $tmp = [];
        if (is_array($res) && $res['code'] == 1){
            foreach ($res['data'] as $v) {
                $tmp[$v['coin_code']] = $v['coin_rate'];
            }
        }
       return $tmp;
    }

	function set() {
		return $this->fetch ();
	}
	function onlinecall() {
		$info = model ( 'Member' )->getInfo ( array ([ 'user_id','=',$this->uid ] ) );
		$this->assign ( 'info', $info );
		return $this->fetch ();
	}
}
